package com.cskaoyan.service;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.vo.WxSearchHistoryKeywordVo;
import com.cskaoyan.bean.vo.WxSearchVo;
import com.cskaoyan.mapper.MarketGoodsMapper;
import com.cskaoyan.mapper.MarketKeywordMapper;
import com.cskaoyan.mapper.MarketSearchHistoryMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

/**
 * @author yi
 * @date 2022/10/14 22:03
 */

@Service
public class WxSearchServiceImpl implements WxSearchService {
    @Autowired
    MarketKeywordMapper marketKeywordMapper;
    @Autowired
    MarketSearchHistoryMapper marketSearchHistoryMapper;
    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    @Override
    public WxSearchVo queryIndex() {
        WxSearchVo wxSearchVo = new WxSearchVo();
        // 三个搜索都要注意筛选条件deleted=false

        // 获取默认关键词
        MarketKeywordExample defaultKeywordExample = new MarketKeywordExample();

        MarketKeywordExample.Criteria defaultKeywordExampleCriteria = defaultKeywordExample.createCriteria();
        defaultKeywordExampleCriteria.andDeletedEqualTo(false);
        defaultKeywordExampleCriteria.andIsDefaultEqualTo(true);

        List<MarketKeyword> marketKeywords = marketKeywordMapper.selectByExample(defaultKeywordExample);
        MarketKeyword defaultKeyword = marketKeywords.get(0);
        wxSearchVo.setDefaultKeyword(defaultKeyword);

        // 获取热门关键词
        MarketKeywordExample hotKeywordExample = new MarketKeywordExample();
        // 按sort_order排序，大的在前，倒序，及其他筛选条件
        hotKeywordExample.setOrderByClause("sort_order desc");
        MarketKeywordExample.Criteria hotKeywordExampleCriteria = hotKeywordExample.createCriteria();
        hotKeywordExampleCriteria.andDeletedEqualTo(false);
        hotKeywordExampleCriteria.andIsHotEqualTo(true);

        List<MarketKeyword> hotKeywords = marketKeywordMapper.selectByExample(hotKeywordExample);

        wxSearchVo.setHotKeywordList(hotKeywords);

        // 获取历史关键词
        MarketSearchHistoryExample searchHistoryExample = new MarketSearchHistoryExample();
        // 获取当前用户id
        Subject subject = SecurityUtils.getSubject();
        PrincipalCollection principals = subject.getPrincipals();
        // 如果不登陆就不返回历史搜索
        if (principals != null) {
            MarketUser user = (MarketUser)principals.getPrimaryPrincipal();
            Integer userId = user.getId();

            // 更新时间新的放前面，以及其他筛选条件
            searchHistoryExample.setOrderByClause("update_time desc");
            MarketSearchHistoryExample.Criteria searchHistoryExampleCriteria = searchHistoryExample.createCriteria();
            searchHistoryExampleCriteria.andDeletedEqualTo(false);
            searchHistoryExampleCriteria.andUserIdEqualTo(userId);

            //select
            List<MarketSearchHistory> searchHistories = marketSearchHistoryMapper.selectByExample(searchHistoryExample);

            // 转换成Vo
            List<WxSearchHistoryKeywordVo> historyKeywordVoList = new LinkedList<>();
            for (MarketSearchHistory searchHistory : searchHistories) {
                WxSearchHistoryKeywordVo wxSearchHistoryKeywordVo = new WxSearchHistoryKeywordVo();
                wxSearchHistoryKeywordVo.setKeyword(searchHistory.getKeyword());
                historyKeywordVoList.add(wxSearchHistoryKeywordVo);
            }

            wxSearchVo.setHistoryKeywordList(historyKeywordVoList);
        }



        return wxSearchVo;
    }

    @Override
    public List<String> queryGoodsName(String keyword) {
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = marketGoodsExample.createCriteria();
        criteria.andNameLike("%" + keyword + "%");
        criteria.andDeletedEqualTo(false);
        List<MarketGoods> marketGoods = marketGoodsMapper.selectByExample(marketGoodsExample);
        List<String> goodsNameList = new LinkedList<>();
        for (MarketGoods marketGood : marketGoods) {
            goodsNameList.add(marketGood.getName());
        }
        return goodsNameList;
    }

    @Override
    public void clearHistory() {
        MarketSearchHistory marketSearchHistory = new MarketSearchHistory();
        marketSearchHistory.setDeleted(true);

        marketSearchHistoryMapper.updateByExampleSelective(marketSearchHistory, null);
    }

}
